<template>
  <BasicModal v-bind="$attrs" @register="register" :title="getTitle" width="1000px">
    <a-spin :spinning="loading" size="large" tip="Loading...">
      <a-form ref="formRef" :disabled="disabled" class="antd-modal-form">
        <p style="text-align: center">
          <span style="width: 35%; text-align: center; font: 1.7em sans-serif">{{ itemName }}检测原始记录</span>
        </p>
        <div v-if="formData.labRecordSamList.length > 0">
          <labSamTable ref="registerSamTable" :labRecordSamList="formData.labRecordSamList"></labSamTable>
        </div>
        <!-- <div v-if="formData.labRecordDevList.length > 0"> -->
        <labDevTable ref="registerDevTable" :labRecordDevList="formData.labRecordDevList"></labDevTable>
        <!-- </div> -->
        <div v-if="formData.labRecordNormList.length > 0">
          <labNormTable ref="registerNormTable" :labRecordNormList="formData.labRecordNormList"></labNormTable>
        </div>
        <div v-if="formData.labRecordTargList.length > 0">
          <labTargTable ref="registeTargTable" :labRecordTargList="formData.labRecordTargList"></labTargTable>
        </div>
        <div v-if="formData.labRecordPerList.length > 0">
          <labPerTable ref="registerPerTable" :labRecordPerList="formData.labRecordPerList"></labPerTable>
        </div>
      </a-form>
    </a-spin>
    <template v-slot:footer>
      <a-button type="default" @click="closeModal"> 取消</a-button>
      <a-button type="danger" @click="auditModal('no')">不通过</a-button>
      <a-button type="primary" @click="auditModal('yes')">通过</a-button>
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { ref, reactive } from 'vue';
  import labSamTable from './subTables/LabCheckItemsSam.vue';
  import labDevTable from './subTables/LabCheckItemsDev.vue';
  import labNormTable from './subTables/LabCheckItemsNorm.vue';
  import labTargTable from './subTables/LabCheckItemsTarget.vue';
  import labPerTable from './subTables/LabCheckItemsPerson.vue';
  import { queryItemData, auditCheckData } from './LabCheckItemsList.api';
  import { judgeHasSign } from '../wentrust/LabEntrust.api';
  // import { useMessage } from '/@/hooks/web/useMessage';
  import { Modal } from 'ant-design-vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useUserStore } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();

  const { userInfo } = useUserStore();
  const registerSamTable = ref();
  const registerDevTable = ref();
  const registerNormTable = ref();
  const registeTargTable = ref();
  const registerPerTable = ref();
  const loading = ref(true);
  const itemName = ref('');
  const formRef = ref();
  const disabled = ref(false);
  const emit = defineEmits(['success']);
  // const { createMessage } = useMessage();
  const getTitle = ref('检测原始记录');
  const formData = reactive<Record<string, any>>({
    entrustId: '',
    taskItemId: '',
    itemId: '',
    auditId: '',
    auditName: '',
    labRecordSamList: [],
    labRecordDevList: [],
    labRecordNormList: [],
    labRecordTargList: [],
    labRecordPerList: [],
  });

  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    setModalProps({
      minHeight: 950,
      showOkBtn: !data.disabled,
      // showOkBtn: data.isUpdate
    });
    disabled.value = data.disabled;
    itemName.value = data.record.itemName;
    formData.entrustId = data.record.entrustId;
    formData.taskItemId = data.record.id;
    formData.itemId = data.record.itemId;
    formData.auditId = data.record.auditId;
    formData.auditName = data.record.auditName;

    let params = {
      entrustId: data.record.entrustId,
      taskItemId: data.record.id,
      itemId: data.record.itemId,
    };
    queryItemData(params)
      .then((res) => {
        formData.labRecordSamList = res.labRecordSamList;
        formData.labRecordDevList = res.labRecordDevList;
        formData.labRecordNormList = res.labRecordNormList;
        formData.labRecordTargList = res.labRecordTargList;
        formData.labRecordPerList = res.labRecordPerList;
      })
      .finally(() => {
        loading.value = false;
      });
  });

  async function auditModal(data) {
    if (data == 'yes') {
      Modal.confirm({
        title: '是否确认通过?',
        async onOk() {
          //更新签名
          // updateSign();
          //提交表单数据
          let params = {
              entrustId: formData.entrustId,
              itemId: formData.itemId,
              itemStatus: '已实验',
              taskItemId: formData.taskItemId,
            };
            console.log('params',params)
          await auditCheckData(params);
          closeModal();
          emit('success');
        },
        onCancel() {
          console.log('取消通过');
        },
      });
    } else if (data == 'no') {
      let params = {
        entrustId: formData.entrustId,
        itemId: formData.itemId,
        itemStatus: '待实验',
        taskItemId: formData.taskItemId,
      };
      Modal.confirm({
        title: '是否确认不通过?',
        async onOk() {
          await auditCheckData(params);
          closeModal();
          emit('success');
        },
        onCancel() {
          console.log('取消不通过');
        },
      });
    }
  }

  // 更新签名
  function updateSign() {
    let signature = '/entrust/labEntrust/signature';
    let servestoken = '/entrust/labEntrust/serveToken';
    //获取服务token和员工票据,员工id
    defHttp.get({ url: servestoken }, { isTransformResponse: false }).then((srcs) => {
      //员工id
      let yaunid = srcs.cardid;
      //let yaunid = 43;
      //服务token
      let tokenServe = srcs.tokenServe;
      //let tokenServe = "eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1MDcyNzUxNDc0NDM0Mzc1NjgiLCJhdWQiOiJiYXNlIiwiaXNzIjoiemh1bnplIiwiZXhwIjoxNzAxMzQwNzgzLCJpYXQiOjE3MDEzMzI5ODN9.f4zHoRe2Z0lvE5HTmRQUqOmlYP9ygQhC_X8icNHhsuY";
      //员工票据
      let employeeTicket = srcs.employeeTicket;
      //let employeeTicket = "22a64aa46829dacaca9f4e10221de1b215be653d";
      //封装请求
      let datae = {
        businessCreateUserId: yaunid,
        businessCreateUserPositionId: '',
        content: 'flowlame=12',
        description: '',
        name: '12',
      };
      //调用签证
      //获取签名
      window.SCZZ_SIGN.open({
        token: employeeTicket, //员工票据，通过获取员工票据接口获取得到
        data: datae, //签证数据,用于签证校验，其内容见下文
        type: 'PERSONAL_SIGNATURE', //类型（个人签名:PERSONAL_SIGNATURE，单位签章:CORP_SIGNATURE），根据不同的类型打开对应的签证页面
        mobile: false, //移动端调用时需要传递
        call: async function (res) {
          console.log('更新签名弹出框返回数据', res);
          if (res.code === 1000) {
            if (res.data.imageUrl) {
              // 把签名存在数据库
              let urls = signature + '?id=' + yaunid + '&paramasa=' + res.data.imageUrl;
              defHttp.post({ url: urls }, { isTransformResponse: false });
              //提交表单数据
              let params = {
                entrustId: formData.entrustId,
                itemId: formData.itemId,
                itemStatus: '已实验',
              };
              await auditCheckData(params);
              closeModal();
              emit('success');
            } else {
              createMessage.error('签名失败，请重新签名！');
            }
          }

          //回调，接收一个结果对象，该对象里包含sdk的响应状态码code，响应信息msg，当响应code为1000时有响应数据data，响应数据内容见下文
        },
      });
    });
  }

  //提交表单数据
  // async function handleSubmit() {
  //     formData.auditId = registerPerTable.value.formPerData.auditId;
  //     formData.auditName = registerPerTable.value.formPerData.auditName;
  //     if (formData.auditName != "" && formData.auditName != null) {
  //         formData.labRecordSamList = registerSamTable.value.props.labRecordSamList;
  //         formData.labRecordDevList = registerDevTable.value.props.labRecordDevList;
  //         formData.labRecordNormList = registerNormTable.value.props.labRecordNormList;
  //         formData.labRecordTargList = registeTargTable.value.props.labRecordTargList;
  //         formData.labRecordPerList = registerPerTable.value.props.labRecordPerList;
  //         await saveOrUpdate(formData).then((res) => {
  //             createMessage.success(res.message);
  //             closeModal();
  //             emit('success');
  //         });
  //     } else {
  //         createMessage.warning("请选择复核人员!");
  //     }
  // }
</script>

<style scoped>
  .header {
    line-height: 35px;
    background-color: #f2f2f2;
    color: #666;
  }

  .tr {
    height: 50px;
    border: 1px solid #a8aeb2;
  }

  .thA {
    width: 20%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thB {
    width: 30%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thC {
    width: 8%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thD {
    width: 32%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thE {
    width: 25%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thF {
    width: 75%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .input {
    border: none;
    background-color: inherit;
    outline: none;
    border-bottom: none;
  }
</style>
